﻿using Gengens.Disk.WebApi.dto;
using Gengens.Disk.WebApi.dto.output;
using Gengens.Disk.WebApi.util;
using Microsoft.AspNetCore.Mvc;

namespace Gengens.Disk.WebApi.Controllers
{
    /// <summary>
    /// 封装了用户信息的控制类
    /// </summary>
    [Route("api/[controller]")]
    public class BaseUserController : ControllerBase
    {
        public event Action NotLoginEvent;
        private void OnNotLogin() {
            if (NotLoginEvent == default) {
                throw new Exception("请先登录");
            }
            NotLoginEvent?.Invoke();
        }

        public string? Token 
        {
            get {
               string? token = Request?.Headers["Authorization"];
                if (string.IsNullOrEmpty(token)) {
                    OnNotLogin();
                }
                return token;
            }
            set => HttpContext.Request.Headers["Authorization"] = value;
        }

        public LoginInfo LoginInfo
        {
            get 
            {
                if (string.IsNullOrWhiteSpace(Token)) return default;
                return LoginUtils.GetLogInfoFromString(Token);
            }
        }

        public Guid UserId => LoginInfo.UserId;
        public string Username => LoginInfo.Username;
        
        
            
    }
}
